package com.alison;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Properties;

// 假设按照年份分表，比如2024年的数据存到表t_order_2024这种形式，示例仅为示意，按实际需求完善逻辑
public class CustomTableShardingAlgorithm implements StandardShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        int hashCode = shardingValue.getValue().hashCode();
        if(hashCode %2==0){
            return "t_order0";
        }
        return "t_order1";
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
        Collection<String> result = new LinkedHashSet<>();
//        for (int i = shardingValue.getValueRange().lowerEndpoint(); i <= shardingValue.getValueRange().upperEndpoint(); i++) {
//            result.add("t_order_" + i);
//        }
        return result;
    }

    @Override
    public void init() {
        // 初始化逻辑，按需从配置读取参数等
    }

    @Override
    public String getType() {
        return "CUSTOM_TABLE_SHARDING";
    }
}